package org.example.camundaapp.service;

import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.DelegationState;
import org.camunda.bpm.engine.task.Task;
import org.example.camundaapp.dto.TaskDTO;
import org.example.camundaapp.utils.ProcessDiagramUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: ProcessService
 * @author: ZHRJ
 * @date: 2025-06-26 11:27
 * @Version: 1.0
 * @description:
 */
@Service
public class ProcessService {

    private final CamundaEngineService camundaEngineService;

    @Autowired
    public ProcessService(CamundaEngineService camundaEngineService) {
        this.camundaEngineService = camundaEngineService;
    }



    /**
     * 启动流程实例
     * @param processKey 流程定义的key
     * @param variables 流程变量
     * @return 流程实例
     */
    public ProcessInstance startProcessInstance(String processKey, Map<String, Object> variables) {
        return camundaEngineService.startProcessInstance(processKey, variables);
    }

    /**
     * 启动流程实例 （带业务 key，推荐）
     * @param processDefinitionKey 流程定义 key
     * @param businessKey 业务 key
     * @param variables 流程变量
     * @return 流程实例
     */
    public ProcessInstance startProcessInstance(String processDefinitionKey, String businessKey, Map<String, Object> variables){
        return camundaEngineService.startProcessInstance(processDefinitionKey, businessKey, variables);
    }

    // 挂起流程实例
    public void suspendProcessInstance(String processInstanceId) {
        camundaEngineService.suspendProcess(processInstanceId);
    }
    // 激活流程实例
    public void activateProcessInstance(String processInstanceId) {
        camundaEngineService.activateProcess(processInstanceId);
    }
    // 删除流程实例
    public void deleteProcessInstance(String processInstanceId, String reason) {
        camundaEngineService.deleteProcess(processInstanceId, reason);
    }

    /**
     * 获取流程状态
     * @param processInstanceId
     * @return 流程状态：RUNNING-运行, SUSPENDED-暂停, COMPLETED-完成, NOT_FOUND-未找到
     */
    public String getProcessStatus(String processInstanceId) {
        return camundaEngineService.getProcessStatus(processInstanceId);
    }
}
